import React, { useEffect, useRef, useState } from 'react';
import { ActionType, ProColumns, ProFormInstance, ProTable } from '@ant-design/pro-components';
// import { getLogSetupCardList } from './request';
import ModalDialog from '@/pages/ChecknHistory/components/ModalDialog';
import { getlogLadderControl } from './requst';
import { Button } from 'antd';
import { useIntl } from '@umijs/max';
import { Read_Card } from '@/utils/Send';
export const waitTimePromise = async (time: number = 100) => {
    return new Promise((resolve) => {
        setTimeout(() => {
            resolve(true);
        }, time);
    });
};

export const waitTime = async (time: number = 100) => {
    await waitTimePromise(time);
};

// 定义表格数据源类型
export type GithubIssueItem = {
    id: number;
    hid: number;
    roomId: number;
    roomNo: string;
    roomName: string;
    cardNum: string;
    cardName: string;
    issuingTime: string;
    cardType: string;
    isTimeoutAlarm: number;
    isFalseLockAlarm: number;
    createBy: string;
    createAt: number;
};


const LadderControl: React.FC = () => {

    const intl = useIntl()

    const actionRef = useRef<ActionType>();
    // 控制对话框显示隐藏
    const [visible, setVisible] = useState(false);

    // 对话框展示的那一行数据信息
    const [form] = useState<GithubIssueItem | null>(null);



    const ref = useRef<ProFormInstance>();

  const readCard = async () => {
    let data = await Read_Card();

    ref.current?.setFieldsValue({ cardNum: data.cardid });
  };

    // 表格数据--列展示信息
    const columns: ProColumns<GithubIssueItem>[] = [
        {
            title: intl.formatMessage({id:"Administer.serial"}),
            dataIndex: 'inedx',
            valueType: 'index',
            width: 60,
        },
        {
            title: intl.formatMessage({id:"Administer.Building"}),
            dataIndex: 'bname',
            search: false,
            align:"center"
        },

        {
            title: intl.formatMessage({id:"Administer.startFloor"}),
            dataIndex: 'startFloor',
            search: false,
        },
       
        {
            title:intl.formatMessage({id:"Administer.publicFloor"}),
            dataIndex: 'publicFloor',
            search: false,
        },
        {
            title: intl.formatMessage({id:"Administer.number"}),
            dataIndex: 'cardNum',
            search: false,
          },
          {
            title: (
              <Button type="primary" onClick={readCard}>
               {intl.formatMessage({id:"Administer.Read card"})}
              </Button>
            ),
            dataIndex: 'cardNum',
            hideInTable: true,
            valueType: 'text',
            key: 'cardNum',
          },
        
        {
            title: intl.formatMessage({id:"Administer.permissions"}),
            dataIndex: 'permissions',
            search: false,
            valueEnum: {
                1: {
                    text: intl.formatMessage({id:"Administer.Turn on the direct access feature"}),
                },
                2: {
                    text: intl.formatMessage({id:"Administer.After swiping the card"}),
                },
                3: {
                    text: intl.formatMessage({id:"Administer.Press all floors"}),
                },
            },
        },
      
        {
            title: intl.formatMessage({id:"Administer.Setup card type"}),
            dataIndex: 'cardType',
            search: false,
            valueEnum: {
                1: {
                    text: intl.formatMessage({id:"Administer.Initial setup card"}),
                },
                2: {
                    text: intl.formatMessage({id:"Administer.Update settings card"}),
                },
            },
        },
        {
            title: intl.formatMessage({id:"Administer.Card issuance time"}),
            dataIndex: 'issuingTime',
            search:false
        },
        {
            title: intl.formatMessage({id:"Administer.Card issuance time"}),
            dataIndex: 'issuingTime',
            valueType: 'dateRange',
            hideInTable: true,
            search: {
              transform: (value) => {
                return {
                  issuing_start_time: value[0],
                  issuing_end_time: value[1],
                };
              },
            },
        },
        {
            title: intl.formatMessage({id:"Administer.Card issuer"}),
            dataIndex: 'createdBy',
            search: false,
        },
        // {
        //   title: '操作',
        //   align: 'center',
        //   valueType: 'option',
        //   key: 'option',
        //   search: false,
        //   width: '200px',
        //   // eslint-disable-next-line @typescript-eslint/no-unused-vars
        //   render: (_text: any, record: GithubIssueItem) => (
        //     <>
        //       <Button
        //         type="link"
        //         onClick={() => {
        //           operate(record);
        //         }}
        //       >
        //         查看
        //       </Button>
        //     </>
        //   ),
        // },
    ];
    return (
        <>
            <ProTable<GithubIssueItem>
                defaultSize={'large'}
                columns={columns}
                actionRef={actionRef}
                formRef={ref}
                cardBordered={false}
                pagination={{
                    defaultPageSize: 10,
                    showSizeChanger: true,
                }}
                request={async (params) => {
                    await waitTime(500);
                    const res: any = await getlogLadderControl({
                        ...params,
                        page: params.current,
                    });
                    return {
                        data: res.data.list || [],
                        success: res.code === 0 ? true : false,
                        total: res.data.totalCount,
                    };
                }}
                rowKey="id"
                search={{ labelWidth: 'auto' }}
                options={{
                    setting: {
                        listsHeight: 400,
                    },
                }}
                dateFormatter="string"
            />
            {visible && (
                <ModalDialog
                    visible={visible}
                    close={() => {
                        setVisible(false);
                    }}
                    form={form}
                />
            )}
        </>
    );
};

export default LadderControl;
